package com.example.bomc.service.fixStaffService.impl;

import com.example.bomc.common.msg.CodeMsg;
import com.example.bomc.dao.FixDao;
import com.example.bomc.model.FixStaff;
import com.example.bomc.model.Trouble;
import com.example.bomc.service.fixStaffService.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StaffImpl implements StaffService {

    @Autowired
    FixDao fixDao;

    @Override
    public CodeMsg selectByFixName(String name,String password) {
        CodeMsg msg = new CodeMsg();
        if (name.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("用户名为空！！！");
            return msg;
        }
        FixStaff staff = fixDao.selectByFixName(name);
        if (staff == null){
            msg.setRetCode(-1);
            msg.setMessage("用户不存在！！！");
            return msg;
        }

        if (password != staff.getPassword()){
            msg.setRetCode(-1);
            msg.setMessage("密码错误！！！");
            return msg;
        }
        msg.setRetCode(0);
        msg.setMessage("登录成功！！！");
        return msg;
    }

    @Override
    public CodeMsg fixTrouble(String tag, int id) {
        CodeMsg msg = new CodeMsg();
        if (tag.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("tag为空！！！");
            return msg;
        }
        fixDao.fixTrouble(tag,id);
        msg.setRetCode(0);
        msg.setMessage("解决问题success！！！");
        return msg;
    }

    @Override
    public CodeMsg selAllTrouble() {
        CodeMsg msg = new CodeMsg();
        List<FixStaff> list = new ArrayList<>();
        list = fixDao.selAllTrouble();
        msg.setRetCode(0);
        msg.setLl(list);
        msg.setMessage("成功！！！");
        return msg;
    }

    @Override
    public CodeMsg selTroubleByTrouble(String trouble) {
        CodeMsg msg = new CodeMsg();
        if (trouble.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("trouble为空！！！");
            return msg;
        }
        List<FixStaff> list = new ArrayList<>();
        list = fixDao.selTroubleByTrouble(trouble);
        msg.setRetCode(0);
        msg.setLl(list);
        msg.setMessage("成功！！！");
        return msg;
    }

    @Override
    public CodeMsg addSameTrouble(Trouble trouble) {
        CodeMsg msg = new CodeMsg();
        if (trouble == null){
            msg.setRetCode(-1);
            msg.setMessage("trouble为空！！！");
            return msg;
        }
        FixStaff staff = fixDao.selectByFixName(trouble.getTrouble());
        if (staff != null){
            msg.setRetCode(-1);
            msg.setMessage("请勿重复提交！！！");
            return msg;
        }
        fixDao.addSameTrouble(trouble);
        msg.setRetCode(0);
        msg.setMessage("成功！！！");
        return msg;
    }

    @Override
    public CodeMsg updateTrouble(String reply, int id) {
        CodeMsg msg = new CodeMsg();
        if (reply.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("trouble为空！！！");
            return msg;
        }
        fixDao.updateTrouble(reply,id);
        msg.setRetCode(0);
        msg.setMessage("成功！！！");
        return msg;
    }

    @Override
    public CodeMsg deleteStaff(String fixname) {
        CodeMsg msg = new CodeMsg();
        if (fixname.isEmpty()){
            msg.setRetCode(-1);
            msg.setMessage("fixname为空！！！");
            return msg;
        }
        fixDao.deleteStaff(fixname);
        msg.setRetCode(0);
        msg.setMessage("成功！！！");
        return msg;
    }
}
